package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import com.limao.DBDataClass.CrowdCompanyDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class CrowdCompanyProvider {

	private static CrowdCompanyProvider instance;

	public static CrowdCompanyProvider getInstance() {
		if (instance == null)
			instance = new CrowdCompanyProvider();
		return instance;
	}

	private CrowdCompanyProvider() {

	}

	private CrowdCompanyDataClass encapOrderData(ResultSet rs) throws Exception {

		CrowdCompanyDataClass orderData = new CrowdCompanyDataClass();
		orderData.companyid = rs.getString("companyid");
		orderData.userid = rs.getString("userid");
		orderData.iscompany = rs.getString("iscompany");
		orderData.companylogo = rs.getString("companylogo");
		orderData.companyname = rs.getString("companyname");
		orderData.companyman = rs.getString("companyman");
		orderData.mancardimg0 = rs.getString("mancardimg0");
		orderData.mancardimg1 = rs.getString("mancardimg1");
		orderData.companycardimg = rs.getString("companycardimg");
		orderData.address = rs.getString("address");
		orderData.tel = rs.getString("tel");
		orderData.telman = rs.getString("telman");
		orderData.intro = rs.getString("intro");
		orderData.introimg = rs.getString("introimg");
		orderData.state = rs.getString("state");
		orderData.createtime = rs.getString("createtime");
		orderData.updatetime = rs.getString("updatetime");
		orderData.refusedreason = rs.getString("refusedreason");
		
		orderData.mancardno = rs.getString("mancardno");
		orderData.companyno = rs.getString("companyno");
		orderData.qq = rs.getString("qq");

		

		return orderData;
	}

	public boolean add_Company(String userid, String companylogo,
			String iscompany, String companyname, String companyman,
			String mancardimg0, String mancardimg1, String companycardimg,
			String address, String tel, String telman, String intro,
			String introimg,String qq,String mancardno,String companyno) {
		boolean flage = false;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con
					.prepareStatement("INSERT into cd_sys_company (userid,companylogo,iscompany,companyname,companyman,mancardimg0,mancardimg1,companycardimg,"
							+ "address,tel,telman,intro,introimg,state,createtime,qq,mancardno,companyno) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

			pstmt.setInt(1, CacheClass.getIntValue(userid));
			pstmt.setString(2, companylogo);
			pstmt.setInt(3, CacheClass.getIntValue(iscompany));

			if (CacheClass.isEmpty(companyname)) {
				pstmt.setNull(4, Types.VARCHAR);
			} else {
				pstmt.setString(4, companyname.trim());
			}

			pstmt.setString(5, companyman);
			pstmt.setString(6, mancardimg0);
			pstmt.setString(7, mancardimg1);

			if (CacheClass.isEmpty(companycardimg)) {
				pstmt.setNull(8, Types.VARCHAR);
			} else {
				pstmt.setString(8, companycardimg.trim());
			}

			pstmt.setString(9, address);

			pstmt.setString(10, tel);
			pstmt.setString(11, telman);
			
			if (CacheClass.isEmpty(intro)) {
				pstmt.setNull(12, Types.VARCHAR);
			} else {
				pstmt.setString(12, intro.trim());
			}
			
			if (CacheClass.isEmpty(introimg)) {
				pstmt.setNull(13, Types.VARCHAR);
			} else {
				pstmt.setString(13, introimg.trim());
			}


			pstmt.setInt(14, 1); // 店铺状态(0禁用,1待审中,2审核未过，3正常,4打烊)

			pstmt.setLong(15, CacheClass.getTime());

			if (CacheClass.isEmpty(qq)) {
				pstmt.setNull(16, Types.VARCHAR);
			} else {
				pstmt.setString(16, qq.trim());
			}
			
			pstmt.setString(17, mancardno.trim());
			
			if (CacheClass.isEmpty(companyno)) {
				pstmt.setNull(18, Types.VARCHAR);
			} else {
				pstmt.setString(18, companyno.trim());
			}
			
			
			pstmt.execute();

			flage = true;

		} catch (Exception e) {
			e.printStackTrace();
			flage = false;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

		return flage;
	}

	 public boolean  add_CompanyPay(int companyid,int paytype,float authcompayrmb,String payimage)
	 {
			boolean flage = false;
			Connection con = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("INSERT into cd_authcompany_pay (companyid,paytype,rmb,payimage,state,createtime) VALUES(?,?,?,?,?,?)");
				pstmt.setInt(1, companyid);
				pstmt.setInt(2, paytype);
				pstmt.setFloat(3, authcompayrmb);
				if(paytype==2) //支付方式 0支付宝  1微信  2线下支付  3银联网上支付     
				{
					pstmt.setString(4, payimage);
					pstmt.setInt(5, 1);  //状态 0待支付 1已支付待确认 2确认已支付 3确认未支付
				}
				else //银联在线支付 (等待支付回调后再改变状记为1)
				{
					pstmt.setNull(4, Types.VARCHAR);
					pstmt.setInt(5, 0);  //状态 0待支付 1已支付待确认 2确认已支付 3确认未支付
					//生成支付参数
					
					
					
				}

				pstmt.setLong(6, CacheClass.getTime());

				pstmt.execute();

				flage = true;

			} catch (Exception e) {
				e.printStackTrace();
				flage = false;
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}

			return flage;		 
		 
		 
		 
		 
		 
	 }
	
	
	
	
	
	public CrowdCompanyDataClass load_CompanyByUserid(int userid) {
		CrowdCompanyDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con
					.prepareStatement("select * from cd_sys_company where userid=? ");
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = encapOrderData(rs);
			}

		} catch (Exception e) {
			e.printStackTrace();
			tmp = null;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
}
